package luogu;

import java.io.*;

public class A1048 {
    static int M=10001;
    static int N=10000010;
    static int n;//采药时间
    static int m;//采药数目
    static int[] w=new int[M];//价值
    static int[] v=new int[M];//体积(时间)
    static long[] dp = new long[N];
    static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        String[] s = read.readLine().split(" ");
        n=Integer.parseInt(s[0]);//背包体积(时间)
        m=Integer.parseInt(s[1]);//物品

        for(int i=1;i<=m;i++){
            String[] s1 = read.readLine().split(" ");
            v[i]=Integer.parseInt(s1[0]);
            w[i]=Integer.parseInt(s1[1]);
        }

        for(int i=1;i<=m;i++){
            for(int j=v[i];j<=n;j++){
                dp[j]=Math.max(dp[j],dp[j-v[i]]+w[i]);
            }
        }
        log.write(Long.toString(dp[n]));
        log.flush();
    }
}
